/*
暴力DFS，没什么难的
不应该困扰于这一类题目
DFS的数组维护考虑要周全
前进、回退、原地踏步等，时刻注意状态值的变化
其次是数据结构，加强构建图和映射关系的能力，加油！
*/
#include <bits/stdc++.h>

using namespace std;

struct node
{
	int cnt=0;
	std::vector<int> v[7];
};

int m,n;
string tmp;

int reactants[110],products[110];
std::vector<std::vector<int> > ans;

node make[110];

void init(){
	for (int i = 0; i < 110; ++i)
	{
		std::vector<int> v(1);
		v[0]=i;
		make[i].v[make[i].cnt]=v;
		make[i].cnt++;
	}
}

void judge(std::vector<vector<int> > &tans){
	for (int i = 0; i < m; ++i)
	{
		for(int j=0;j<ans[i].size()&&j<tans[i].size();++j){
			if(tans[i][j]<ans[i][j]){
				ans=tans;
				return;
			}else if(ans[i][j]<tans[i][j]){
				return;
			}
		}
	}
}

void dfs(int index,int h_reactants[],std::vector<vector<int> > tans){
	if(index==m){
		if(ans.size()==0){
			ans=tans;
		}else{
			judge(tans);
		}
	}
	int id=products[index];
	for (int i = 0; i < make[id].cnt; ++i)
	{
		int ok=1;
		std::vector<int> v,visit;
		for (int j = 0; j < make[id].v[i].size(); ++j)
		{
			int num=make[id].v[i][j];
			if(h_reactants[num]){
				v.emplace_back(num);
				h_reactants[num]=0;
				visit.emplace_back(num);
			}else{
				ok=0;
				break;
			}
		}
		if(ok){
			tans.emplace_back(v);
			dfs(index+1,h_reactants,tans);
			tans.pop_back();
			for(int i=0;i<visit.size();i++){
				int num=visit[i];
				h_reactants[num]=1;
			}
		}else{
			for(int i=0;i<visit.size();i++){
				int num=visit[i];
				h_reactants[num]=1;
			}
		}
	}
}

int main(int argc, char const *argv[])
{
	int h_reactants[100]={0};
	scanf("%d",&n);
	for (int i = 0; i < n; ++i)
	{
		int tmp;scanf("%d",&tmp);
		reactants[i]=tmp;
		h_reactants[tmp]=1;
	}

	scanf("%d",&m);
	for (int i = 0; i < m; ++i)
	{
		int tmp;scanf("%d",&tmp);
		products[i]=tmp;
	}

	int k;scanf("%d",&k);
	getchar(); 
	for (int i = 0; i < k; ++i)
	{
		getline(cin,tmp,'\n');
		std::vector<int> v;
		for (int i = 0; i < tmp.size(); )
		{
			if(tmp[i]>='0'&&tmp[i]<='9'){
				int num=stoi(tmp.substr(i,2));
				v.emplace_back(num);
				i+=2;
			}else{
				i++;
			}
		}
		int num =v[v.size()-1];
		v.pop_back();
		make[num].v[make[num].cnt]=v;
		make[num].cnt++;
	}

	init();
	vector<vector<int> > tans;
	dfs(0,h_reactants,tans);

	for (int i = 0; i < ans.size(); ++i)
	{
		for(int j=0;j<ans[i].size();++j){
			printf("%02d", ans[i][j]);
			if(j!=ans[i].size()-1){
				printf(" + ");
			}else{
				printf(" -> ");
			}
		}
		printf("%02d\n",products[i]);
	}
	return 0;
}